import { NextRequest, NextResponse } from 'next/server';
import prisma from '@/lib/prisma';
import { ResponseUtil } from '@/utils/response';
import { verifyAuth } from '@/utils/auth';
import { canModerateContent } from '@/utils/permissions';
import { ClientUser } from '@/utils/client-auth';

// 根据排序类型获取排序规则
function getOrderBy(sort: string) {
  switch (sort) {
    case 'hot': // 热门 - 按浏览量排序
      return [
        { viewCount: 'desc' as const },
        { createdAt: 'desc' as const },
      ];
    case 'popular': // 热帖 - 按点赞数排序
      return [
        { likeCount: 'desc' as const },
        { createdAt: 'desc' as const },
      ];
    case 'essence': // 精华 - 按精华状态排序
      return [
        { isEssence: 'desc' as const },
        { createdAt: 'desc' as const },
      ];
    case 'top': // 置顶 - 按置顶状态排序
      return [
        { isTop: 'desc' as const },
        { createdAt: 'desc' as const },
      ];
    case 'latest': // 最新 - 按创建时间排序
    default:
      return [
        { createdAt: 'desc' as const },
      ];
  }
}

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const sectionId = parseInt(id);
    const { searchParams } = new URL(request.url);
    const page = parseInt(searchParams.get('page') || '1');
    const pageSize = parseInt(searchParams.get('pageSize') || '10');
    const subsectionId = searchParams.get('subsectionId');
    const sort = searchParams.get('sort') || 'latest'; // 添加排序参数
    const skip = (page - 1) * pageSize;
    
    if (isNaN(sectionId)) {
      return ResponseUtil.error('无效的板块ID');
    }

    // 获取当前用户信息
    const { user } = await verifyAuth(request);
    
    // 构建查询条件
    let whereCondition: any = {};
    
    if (subsectionId) {
      // 如果指定了子版块，则筛选该子版块的帖子
      whereCondition.sectionId = parseInt(subsectionId);
    } else {
      // 否则筛选当前板块及其所有子版块的帖子
      const childSectionIds = await prisma.forumSection.findMany({
        where: { parentId: sectionId },
        select: { id: true }
      });
      
      const allSectionIds = [sectionId, ...childSectionIds.map(s => s.id)];
      whereCondition.sectionId = { in: allSectionIds };
    }

    // 获取版块信息以确定版主ID
    const sectionInfo = await prisma.forumSection.findUnique({
      where: { id: sectionId },
      select: { moderatorId: true }
    });

    // 根据用户权限过滤帖子状态
    // 如果是版主或超级管理员，显示所有状态的帖子（除了已删除的）
    // 如果是普通用户，只显示已发布的帖子
    if (!user || !canModerateContent(user as unknown as ClientUser, sectionInfo?.moderatorId)) {
      // 普通用户只显示已发布的帖子，且不是已拒绝状态
      whereCondition.isDeleted = false;
      whereCondition.status = { not: 'REJECTED' }; // 排除已拒绝的帖子
    } else {
      // 版主或超级管理员可以看到所有状态的帖子（除了已删除的和已拒绝的）
      whereCondition.isDeleted = false;
      whereCondition.status = { not: 'REJECTED' }; // 排除已拒绝的帖子
    }

    // 根据排序类型添加筛选条件
    switch (sort) {
      case 'top':
        whereCondition.isTop = true; // 只显示置顶帖子
        break;
      case 'essence':
        whereCondition.isEssence = true; // 只显示精华帖子
        break;
      case 'hot':
        // 热门帖子：浏览量大于平均值的帖子
        const avgViewCount = await prisma.forumPost.aggregate({
          where: { ...whereCondition },
          _avg: { viewCount: true }
        });
        if (avgViewCount._avg.viewCount && avgViewCount._avg.viewCount > 0) {
          whereCondition.viewCount = { gte: Math.round(avgViewCount._avg.viewCount) };
        }
        break;
      case 'popular':
        // 热帖：点赞数大于平均值的帖子
        const avgLikeCount = await prisma.forumPost.aggregate({
          where: { ...whereCondition },
          _avg: { likeCount: true }
        });
        if (avgLikeCount._avg.likeCount && avgLikeCount._avg.likeCount > 0) {
          whereCondition.likeCount = { gte: Math.round(avgLikeCount._avg.likeCount) };
        }
        break;
      case 'latest':
        // 最新排序：不添加额外筛选条件，只按时间排序
        break;
      default:
        // 默认最新排序：不添加额外筛选条件
        break;
    }
    console.log(whereCondition,'whereCondition');

    // 查询帖子总数
    const totalPosts = await prisma.forumPost.count({
      where: whereCondition,
    });

    // 查询帖子列表，包含作者信息
    const posts = await prisma.forumPost.findMany({
      where: whereCondition,
      include: {
        author: {
          select: {
            id: true,
            nickname: true,
            avatar: true,
          },
        },
        section: {
          select: {
            id: true,
            name: true,
            moderatorId: true,
          },
        },
      },
      orderBy: getOrderBy(sort), // 根据排序参数设置排序规则
      skip,
      take: pageSize,
    });

    // 处理数据格式
    const formattedPosts = posts.map(post => ({
      id: post.id,
      title: post.title,
      content: post.content,
      authorId: post.authorId,
      author: post.author,
      sectionId: post.sectionId,
      section: post.section,
      viewCount: post.viewCount,
      commentCount: post.commentCount,
      likeCount: post.likeCount,
      isSticky: post.isTop,
      isTop: post.isTop,
      isEssence: post.isEssence,
      isHot: post.isHot,
      isNewbie: post.isNewbie,
      status: post.status,
      createdAt: post.createdAt.toISOString(),
      updatedAt: post.updatedAt.toISOString(),
    }));

    const totalPages = Math.ceil(totalPosts / pageSize);

    return ResponseUtil.success({
      posts: formattedPosts,
      totalPosts,
      totalPages,
      currentPage: page,
      pageSize,
    });
  } catch (error) {
    console.error('获取板块帖子失败:', error);
    return ResponseUtil.error('获取板块帖子失败', 500);
  }
} 